<?php


namespace app\api\controller;

use think\Db;

/**
 * Class 用户定时器
 * @package app\api\controller
 */
class Usertime extends \app\common\controller\Api
{
    protected $noNeedLogin = ['*'];

    /**
     * 团队奖励
     */
    public function team_reward($user){
        $end = strtotime(date('Y-m-d'));//今日开始时间戳
        $start = strtotime(date('Y-m-d',$end-3600));//昨日开始时间戳
//        $sql = "select level,group_concat(id) as ids from fa_user where team_reward_time BETWEEN   {$start} AND  {$end}  group by level ";
//        $user = Db::query($sql);///   这里是最开始单独跑定时器  批量操作用户
//        Array
//        (
//            [0] => Array
//            (
//                [level] => 3
//                [ids] => 1,2
//            )
//        )


        $cb_count = db('cb_fee_count')->whereTime('createtime','yesterday')->value('num');
//        $cc_count = db('cc_fee_count')->whereTime('createtime','yesterday')->value('num');//cc_团队奖预留
        $cc_count = 0;
        if (!$cb_count){
            $cb_count = 0;
        }
        if (!$cc_count){
            $cc_count = 0;
        }
        $a1_num = db('user')
//            ->where('createtime','<',$end)
            ->where('level',1)->count();//昨日a1总人数
        $a2_num = db('user')
//            ->where('createtime','<',$end)
            ->where('level',2)->count();//昨日a2总人数
        $a3_num = db('user')
//            ->where('createtime','<',$end)
            ->where('level',3)->count();//昨日a3总人数
        $a4_num = db('user')
//            ->where('createtime','<',$end)
            ->where('level',4)->count();//昨日a4总人数
        $a5_num = db('user')
//            ->where('createtime','<',$end)
            ->where('level',5)->count();//昨日a5总人数
        $cb_pingtaiincome = db('config')->where('name','cb_all_wang_income')->value('value');//加权分红

//        $cc_pingtaiincome = db('config')->where('name','cc_all_wang_income')->value('value');//加权分红,,cc_团队奖预留

        $cc_pingtaiincome = 0;
        foreach ($user as $key => $value){
            if ($value['level'] == 1){

                //A1等级
                $this->reward_a1($value['ids'],$cb_count,$a1_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome);
            }elseif ($value['level'] == 2){
                //A2等级
                $this->reward_a2($value['ids'],$cb_count,$a2_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome);
            }elseif ($value['level'] == 3){
                //A3等级
                $this->reward_a3($value['ids'],$cb_count,$a3_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome);
            }elseif ($value['level'] == 4){
                //A4等级
                $this->reward_a4($value['ids'],$cb_count,$a4_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome);
            }elseif ($value['level'] == 5){
                //A5等级
                $this->reward_a5($value['ids'],$cb_count,$a5_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome);
            }
        }
    }

    /**
     * A1
     * @param $user_ids 用户们
     * @param $count 总手续费
     * @param $user_num 该等级人数
     */
    public function reward_a1($user_ids,$cb_count,$user_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome){
        $reward_rule = db('uplevel')
            ->where('level','1')
            ->find();
        if ($reward_rule){
            $cb_fee_get_num = round($cb_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cb_income = $reward_rule['weight_fee']/100*$cb_pingtaiincome/$user_num;//每人得到的加权分红

            $cc_fee_get_num = round($cc_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cc_income = $reward_rule['weight_fee']/100*$cc_pingtaiincome/$user_num;//每人得到的加权分红


            if ($cb_fee_get_num + $cb_income > 0 || $cc_fee_get_num + $cc_income > 0){
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->inc('cb_num',$cb_fee_get_num + $cb_income)
                    ->inc('cc_num',$cc_fee_get_num + $cc_income)
                    ->update(['team_reward_time'=>time()]);
                if ($cb_fee_get_num + $cb_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCbwater($v,$cb_fee_get_num + $cb_income,1,'区块节点收益');
                    }
                }
                if ($cc_fee_get_num + $cc_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCcwater($v,$cc_fee_get_num + $cc_income,1,'区块节点收益');
                    }
                }
            }else{
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->update(['team_reward_time'=>time()]);
            }
        }else{
            db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                ->where('id',$user_ids)
                ->update(['team_reward_time'=>time()]);
        }

    }
    /**
     * A2
     */
    public function reward_a2($user_ids,$cb_count,$user_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome){
        $reward_rule = db('uplevel')
            ->where('level','2')
            ->find();
        if ($reward_rule){
            $cb_fee_get_num = round($cb_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cb_income = $reward_rule['weight_fee']/100*$cb_pingtaiincome/$user_num;//每人得到的加权分红

            $cc_fee_get_num = round($cc_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cc_income = $reward_rule['weight_fee']/100*$cc_pingtaiincome/$user_num;//每人得到的加权分红


            if ($cb_fee_get_num + $cb_income > 0 || $cc_fee_get_num + $cc_income > 0){
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->inc('cb_num',$cb_fee_get_num + $cb_income)
                    ->inc('cc_num',$cc_fee_get_num + $cc_income)
                    ->update(['team_reward_time'=>time()]);
                if ($cb_fee_get_num + $cb_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCbwater($v,$cb_fee_get_num + $cb_income,1,'区块节点收益');
                    }
                }
                if ($cc_fee_get_num + $cc_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCcwater($v,$cc_fee_get_num + $cc_income,1,'区块节点收益');
                    }
                }
            }else{
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->update(['team_reward_time'=>time()]);
            }
        }else{
            db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                ->where('id',$user_ids)
                ->update(['team_reward_time'=>time()]);
        }
    }
    /**
     * A3
     */
    public function reward_a3($user_ids,$cb_count,$user_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome){
        $reward_rule = db('uplevel')
            ->where('level','3')
            ->find();
        if ($reward_rule){
            $cb_fee_get_num = round($cb_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cb_income = $reward_rule['weight_fee']/100*$cb_pingtaiincome/$user_num;//每人得到的加权分红

            $cc_fee_get_num = round($cc_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cc_income = $reward_rule['weight_fee']/100*$cc_pingtaiincome/$user_num;//每人得到的加权分红


            if ($cb_fee_get_num + $cb_income > 0 || $cc_fee_get_num + $cc_income > 0){
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->inc('cb_num',$cb_fee_get_num + $cb_income)
                    ->inc('cc_num',$cc_fee_get_num + $cc_income)
                    ->update(['team_reward_time'=>time()]);
                if ($cb_fee_get_num + $cb_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCbwater($v,$cb_fee_get_num + $cb_income,1,'区块节点收益');
                    }
                }
                if ($cc_fee_get_num + $cc_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCcwater($v,$cc_fee_get_num + $cc_income,1,'区块节点收益');
                    }
                }
            }else{
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->update(['team_reward_time'=>time()]);
            }
        }else{
            db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                ->where('id',$user_ids)
                ->update(['team_reward_time'=>time()]);
        }
    }
    /**
     * A4
     */
    public function reward_a4($user_ids,$cb_count,$user_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome){
        $reward_rule = db('uplevel')
            ->where('level','4')
            ->find();
        if ($reward_rule){
            $cb_fee_get_num = round($cb_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cb_income = $reward_rule['weight_fee']/100*$cb_pingtaiincome/$user_num;//每人得到的加权分红

            $cc_fee_get_num = round($cc_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cc_income = $reward_rule['weight_fee']/100*$cc_pingtaiincome/$user_num;//每人得到的加权分红


            if ($cb_fee_get_num + $cb_income > 0 || $cc_fee_get_num + $cc_income > 0){
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->inc('cb_num',$cb_fee_get_num + $cb_income)
                    ->inc('cc_num',$cc_fee_get_num + $cc_income)
                    ->update(['team_reward_time'=>time()]);
                if ($cb_fee_get_num + $cb_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCbwater($v,$cb_fee_get_num + $cb_income,1,'区块节点收益');
                    }
                }
                if ($cc_fee_get_num + $cc_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCcwater($v,$cc_fee_get_num + $cc_income,1,'区块节点收益');
                    }
                }
            }else{
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->update(['team_reward_time'=>time()]);
            }
        }else{
            db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                ->where('id',$user_ids)
                ->update(['team_reward_time'=>time()]);
        }
    }
    /**
     * A5
     */
    public function reward_a5($user_ids,$cb_count,$user_num,$cb_pingtaiincome,$cc_count,$cc_pingtaiincome){
        $reward_rule = db('uplevel')
            ->where('level','5')
            ->find();
        if ($reward_rule){
            $cb_fee_get_num = round($cb_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cb_income = $reward_rule['weight_fee']/100*$cb_pingtaiincome/$user_num;//每人得到的加权分红

            $cc_fee_get_num = round($cc_count * $reward_rule['share_fee']/100/$user_num,2);//每个人得到的手续费分红
            $cc_income = $reward_rule['weight_fee']/100*$cc_pingtaiincome/$user_num;//每人得到的加权分红


            if ($cb_fee_get_num + $cb_income > 0 || $cc_fee_get_num + $cc_income > 0){
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->inc('cb_num',$cb_fee_get_num + $cb_income)
                    ->inc('cc_num',$cc_fee_get_num + $cc_income)
                    ->update(['team_reward_time'=>time()]);
                if ($cb_fee_get_num + $cb_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCbwater($v,$cb_fee_get_num + $cb_income,1,'区块节点收益');
                    }
                }
                if ($cc_fee_get_num + $cc_income > 0){
                    foreach (explode(',',trim($user_ids,'')) as $k => $v){
                        setCcwater($v,$cc_fee_get_num + $cc_income,1,'区块节点收益');
                    }
                }
            }else{
                db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                    ->where('id',$user_ids)
                    ->update(['team_reward_time'=>time()]);
            }
        }else{
            db('user')
//                    ->where('id','in',$user_ids)//最开始批量操作
                ->where('id',$user_ids)
                ->update(['team_reward_time'=>time()]);
        }
    }

    /**
     * 用户等级
     */
    public function userlevel(){
        $userlist = db('user')
            ->field('id,level,level_max')
            ->where('id',6)
            ->whereTime('team_reward_time','yesterday')//上次结算等级发放家里是昨天
            ->limit(1000)
            ->select();

        $i = 0;
        foreach ($userlist as $k => $v){
            $i++;
            $son_ok = db('user')
                ->where('first_id',$v['id'])
                ->where('is_truename','2')
                ->count();//分享有效用户

            $lastlevel = $v['level'] - 1;
            $lastnum = db('user')
                ->where('first_id',$v['id'])
                ->where('level',$lastlevel)
                ->count();//直推上一级用户
            $allusers = db('user')
                ->where('tree','like','%,'.$v['id'].',%')
                ->column('id');
            $allusers[] = $v['id'];
            $active = db('user_mill')
                ->where('user_id','in',$allusers)//团队
                ->where('status','1')//矿机运行中
                ->sum('price');//总活跃值
            $cimill_ids = db('user_mill')
                ->where('user_id',$v['id'])//用户的
                ->where('status','1')//正在运行的
                ->column('cb_mill_id');//所有矿机
            $cimill_ids = array_unique($cimill_ids);//数组去重

            $level = db('uplevel')
                ->where('true_num','<=',$son_ok)//直推有效人数
                ->where('team_st','<=',$active)//团队活跃值
                ->where('up_level_num','<=',$lastnum)//直推上级人数
                ->where('cb_mill_id','in',$cimill_ids)//必须持有的矿机
                ->order('id desc')
                ->value('level');
//            halt($level);
            if (!$level){
                $level = $v['level'];
            }

            if ($level != $v['level']){

                //等级有变化
                if ($v['level_max'] < $level){
                    db('user')->where('id',$v['id'])->update(['level'=>$level,'level_max'=>$level]);
                    uplevel_reward($v['id'],$level);
                }else{
                    db('user')->where('id',$v['id'])->update(['level'=>$level]);
                }
            }
            $userarr = [];
            $userarr[] =['level'=>$level,'ids'=>$v['id']];
            $this->team_reward($userarr);
        }
        echo $i;
    }
}